package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class Give_PermissionsActivity extends Activity {
    private ArrayList<GateWay> user;
    ArrayList<String> s;
    Spinner sp;
    EditText mobile;
    String Mac;
    String name;
    PopupWindow pw;
    int clickPsition = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_give__permissions);
        info();
    }

    public void info() {

        LinearLayout iv = (LinearLayout) findViewById(R.id.iv_my_baojing);

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });
        SharedPreferences sp2 = getSharedPreferences("first", MODE_PRIVATE);
        String UserName = sp2.getString("username", null);
        mobile = (EditText) findViewById(R.id.mobile);
        sp = (Spinner) findViewById(R.id.sp_ato2);
        s = new ArrayList<>();
        MyApplication m = (MyApplication) getApplication();

     //  ---------------------------
        UserClass uc = new UserClass();
        uc.userId = m.userId;
        uc.isAdmin = "1";
        String js = JSON.toJSONString(uc);
        Log.d("jsh",js);
        js = AESUtil.encrypt(js);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setContentType("application/json");
        try {
            params.setBodyEntity(new StringEntity(js, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.canAuthorizeList, params,
                new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        HttpConnection.toastLog("网络错误", Give_PermissionsActivity.this);
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            Log.d("result",arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String status = jb.getString("status");
                            if ("0".equals(status)) {
                                JSONArray ja = jb.getJSONArray("deviceList");
                                user = (ArrayList<GateWay>) JSON.parseArray(ja.toString(), GateWay.class);

                                for (int i = 0; i < user.size(); i++) {
                                    s.add(user.get(i).mac);
                                }
                               Spinneradapter sp5 = new Spinneradapter(s, Give_PermissionsActivity.this);
                                sp.setAdapter(sp5);

                            }else{
                                HttpConnection.toastLog("服务器错误", Give_PermissionsActivity.this);
                            }
                        }else{
                            HttpConnection.toastLog("服务器错误", Give_PermissionsActivity.this);
                        }

                    }
                });

   sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (s.size() != 0) {
                    name = s.get(position);
                    Mac = user.get(position).mac;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

                ToastUtils.showToast(Give_PermissionsActivity.this,"我没有选择");
                if (s.size() != 0) {
                    Mac = s.get(0);
                    Mac = user.get(0).mac;
                }
            }
        });}

    public void give(View v) {
        Log.d("ffffffffffffffff","hhhhhhhhhhh");
        SharedPreferences sp = getSharedPreferences("first", MODE_PRIVATE);
        String username = sp.getString("username", null);
        String smobile = mobile.getText().toString();
        UserClass uc = new UserClass();
        uc.mac = Mac;
        uc.mobileTo = smobile;
        uc.mobileFrom = username;
        uc.name = name;
        uc.type="4";
        String js = JSON.toJSONString(uc);
        Log.d("jsre",js);
        js = AESUtil.encrypt(js);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setContentType("application/json");
        try {
            params.setBodyEntity(new StringEntity(js, "utf-8"));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.deviceAuthorized, params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        HttpConnection.toastLog("网络错误", Give_PermissionsActivity.this);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            Log.d("arg0.gggg",arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String status = jb.getString("status");
                            if ("0".equals(status)) {
                                HttpConnection.toastLog("授权成功", Give_PermissionsActivity.this);
                            } else if ("-2".equals(status)) {
                                HttpConnection.toastLog("请先注册", Give_PermissionsActivity.this);

                            }else if ("2".equals(status)) {
                                HttpConnection.toastLog("该网关已分享", Give_PermissionsActivity.this);
                            }
                        }else{

                            HttpConnection.toastLog("服务器错误", Give_PermissionsActivity.this);
                        }
                    }
                });


    }


}







